//SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package board_dao

import (
	"context"

	"gitcode.com/eteam/api-server/dao/common"
	"gitcode.com/eteam/proto-gen-go.git/board_extdata_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
)

type BasicExtDataInfoDbItem struct {
	Name        string                         `bson:"name"`
	ExtDataType board_extdata_api.EXTDATA_TYPE `bson:"extDataType"`
	ServUrl     string                         `bson:"servUrl"`
	AccessToken string                         `bson:"accessToken"`
	//one of
	GitServType     board_extdata_api.GIT_SERV_TYPE     `bson:"gitServType"`
	CrawlerServType board_extdata_api.CRAWLER_SERV_TYPE `bson:"crawlerServType"`
	MapServType     board_extdata_api.MAP_SERV_TYPE     `bson:"mapServType"`
}

type ExtDataInfoDbItem struct {
	ExtDataId    string                 `bson:"_id"`
	BoardId      string                 `bson:"boardId"`
	BasicInfo    BasicExtDataInfoDbItem `bson:"basicInfo"`
	CreateUserId string                 `bson:"createUserId"`
	CreateTime   int64                  `bson:"createTime"`
	UpdateUserId string                 `bson:"updateUserId"`
	UpdateTime   int64                  `bson:"updateTime"`
}

func (item *ExtDataInfoDbItem) ToExtDataInfo() *board_extdata_api.ExtDataInfo {
	basicInfo := &board_extdata_api.BasicExtDataInfo{
		Name:        item.BasicInfo.Name,
		ExtDataType: item.BasicInfo.ExtDataType,
		ServUrl:     item.BasicInfo.ServUrl,
		AccessToken: item.BasicInfo.AccessToken,
	}
	if item.BasicInfo.ExtDataType == board_extdata_api.EXTDATA_TYPE_EXTDATA_TYPE_GIT {
		basicInfo.ServType = &board_extdata_api.BasicExtDataInfo_GitServType{
			GitServType: item.BasicInfo.GitServType,
		}
	} else if item.BasicInfo.ExtDataType == board_extdata_api.EXTDATA_TYPE_EXTDATA_TYPE_CRAWLER {
		basicInfo.ServType = &board_extdata_api.BasicExtDataInfo_CrawlerServType{
			CrawlerServType: item.BasicInfo.CrawlerServType,
		}
	} else if item.BasicInfo.ExtDataType == board_extdata_api.EXTDATA_TYPE_EXTDATA_TYPE_MAP {
		basicInfo.ServType = &board_extdata_api.BasicExtDataInfo_MapServType{
			MapServType: item.BasicInfo.MapServType,
		}
	}
	return &board_extdata_api.ExtDataInfo{
		ExtDataId:    item.ExtDataId,
		BasicInfo:    basicInfo,
		CreateUserId: item.CreateUserId,
		CreateTime:   item.CreateTime,
		UpdateUserId: item.UpdateUserId,
		UpdateTime:   item.UpdateTime,
	}
}

type _ExtDataInfoDao struct {
	mc *mongo.Collection
}

func newExtDataInfoDao(db *mongo.Database) (*_ExtDataInfoDao, error) {
	mc := db.Collection(common.DB_NS_BOARD + "extdata")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"boardId", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_ExtDataInfoDao{mc: mc}, nil
}

func (db *_ExtDataInfoDao) Insert(ctx context.Context, dbItem *ExtDataInfoDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_ExtDataInfoDao) Exist(ctx context.Context, boardId, extDataId string) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"boardId": boardId, "_id": extDataId})
	return count > 0, err
}

func (db *_ExtDataInfoDao) Get(ctx context.Context, boardId, extDataId string) (*ExtDataInfoDbItem, error) {
	retItem := &ExtDataInfoDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"boardId": boardId, "_id": extDataId}).Decode(retItem)
	return retItem, err
}

func (db *_ExtDataInfoDao) Remove(ctx context.Context, boardId, extDataId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"boardId": boardId, "_id": extDataId})
	return err
}

func (db *_ExtDataInfoDao) List(ctx context.Context, boardId string) ([]*ExtDataInfoDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"boardId": boardId})
	if err != nil {
		return nil, err
	}
	retList := []*ExtDataInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_ExtDataInfoDao) Update(ctx context.Context, boardId, extDataId string, basicItem *BasicExtDataInfoDbItem, updateUserId string, updateTime int64) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": extDataId, "boardId": boardId},
		bson.M{"$set": bson.M{
			"basicInfo":    basicItem,
			"updateUserId": updateUserId,
			"updateTime":   updateTime,
		}})
	return err
}
